/**
 * @class Login
 * @fileOverview 登录控制器
 * @extends yijs.air.Controller
 */ 
var Login = function(){
	//加载用户模型	
	this.model = new User();
	this.errors = [];
	this.lang = yijs.lang.login;
	this.listeners = {submit: null};
	
	this.view();
}
//yijs.extend(Login,yijs.air.Controller);
/**
 * 读取并解析模板
 */
Login.prototype.view = function(){
	var _that = this;
	var viewPath = this.getViewPath();
	var langsXml = yijs.air.view.getLangXml(yijs.lang.login);
	yijs.air.view(langsXml,{url:viewPath},"body",function(xml,xsl){_that.viewHandler(xml,xsl)});
}
/**
 * 解析完模板后触发的事件
 * @param {Object} xml
 * @param {Object} xsl
 */
Login.prototype.viewHandler = function(xml,xsl){
	var _that = this;
	yijs.hover("#login_submit","login_submit_hover");
	yijs.hover(".btn_select","btn_select_hover");
	this.setUserNameSelect();
	this.setBlogSelect();		
	$(".btn_select").click(function(){
		_that.btnSelectClickHanlder($(this));
		
	})		
	$("#login_submit").click(function(){
			_that.submit();
	})
}
/**
 * 创建用户名选择框
 */
Login.prototype.setUserNameSelect = function(){
	var _that = this;
	//获取用户数据
	var data = this.model.get();
	if(data != null && data.length != null){ 
		//用户数据源
		var dataProvider = new yijs.Collection(data);
		//初始化用户选择框
		var list = new yijs.List({renderTo : '#USERNAME_PARENT',
								 dataProvider : dataProvider,
								 labelField : 'name',
								 itemRender : function(itemData){
								 	return ""+Base64.decode(itemData.name)+"<div class='del_user' name='"+itemData.name+"'></div>";
								 },								 
								 position : "absolute",
								 top: "-2",
								 left:35,
								 width:142});
		list.render();			
		
		yijs.hover(".del_user","delUser_hover");
		
		//给删除用户按钮绑定单击事件
		$(".del_user").click(function(e){
			var name = $(this).attr("name");
			_that.model.del(name);
			var index = $('.del_user').index($(this));
			dataProvider.removeItemAt(index);
			dataProvider.length == 0 && list.hide();
			e.stopPropagation();
			
		})
		//给选择框元素绑定单击事件
		list.bind("itemClick",function(evt){
			var _index = evt.index;
			var _data = dataProvider.getItemAt(_index);
			var $li = $(this).parents("li");
			$("#USERNAME").val(Base64.decode(_data.name));
			$("#PASSWORD").val(Base64.decode(_data.password));
			$("#IS_REMEMBER").attr("checked",Boolean(_data.isRemember));
			$("#AUTO_LOGIN").attr("checked",Boolean(_data.autoLogin));
			$("#BLOG_CONTROLLER").val(_data.controller);
			$("#BLOG").val(helpYi.getControllerName(_data.controller));
			var $list = $(this).parents("li").find(".yijs_list");
			$list.slideUp("fast");		
			$(this).find(".del_user").hide();	
		})	
		//给选择框元素绑定鼠标滑过事件
		list.bind("itemMouseover",function(evt){
			var $del = $(evt.that).find(".del_user");
			$del.fadeIn("fast");	
		})	
		//给选择框元素绑定鼠标移开事件	
		list.bind("itemMouseout",function(evt){
			var $del = $(evt.that).find(".del_user");
			$del.fadeOut("fast");
		})				
	}	
}
/**
 * 创建微博选择框
 */
Login.prototype.setBlogSelect = function(){
	//微博选择框数据源
	var dataProviderBlog = new yijs.Collection(yijs.config.BLOGS);
	//初始化微博选择框
	var listBlog = new yijs.List({renderTo : '#BLOG_PARENT',
							 dataProvider : dataProviderBlog,
							 labelField : 'name',							 
							 position : "absolute",
							 top: "-2",
							 left:35,
							 width:142});
	listBlog.render();
	//设置微博输入框默认值（一般是第一项）
	var _data = dataProviderBlog.getItemAt(0);
	setInputVal(_data);	
	listBlog.bind("itemClick",function(evt){
		var _data = evt.data;
		setInputVal(_data);	
		var $list = $(this).parents("li").find(".yijs_list");
		$list.slideUp("fast");				
	})
	//设置微博输入框的值
	function setInputVal(data){
		$("#BLOG").val(data.name);
		$("#BLOG_CONTROLLER").val(data.controller);			
	}	
}
/**
 * 点击选择框下拉按钮触发的事件
 */
Login.prototype.btnSelectClickHanlder = function($this){
		var $list = $this.parents("li").find(".yijs_list");
		if($list.css("display") == "none"){
			$list.slideDown("fast");
		}else{
			$list.slideUp("fast");		
		}	
}
/**
 * 提交表格数据
 */
Login.prototype.submit = function(){
		var _that = this;
		var USERNAME = $("#USERNAME").val();
		var PASSWORD = $("#PASSWORD").val();
		var BLOG = $("#BLOG").val();
		var BLOG_CONTROLLER = $("#BLOG_CONTROLLER").val();
		var IS_REMEMBER = Number($("#IS_REMEMBER:checked").val()) || 0;
		var AUTO_LOGIN = Number($("#AUTO_LOGIN:checked").val()) || 0;
		this.errors = [];
		$("#LOGIN_ERRORS").html("");
		if(USERNAME == "")	this.errors.push(this.lang.errors.emptyUsername);
		if(PASSWORD == "")	this.errors.push(this.lang.errors.emptyPassword);
		if(BLOG_CONTROLLER == "")	this.errors.push(this.lang.errors.emptyBlogController);
		
		if(this.errors.length == 0){	
			var Authorization = "Basic " + Base64.encode(USERNAME+":"+PASSWORD);
			var event = {target : this,data : {authorization : Authorization,name:USERNAME,password:PASSWORD,controller:BLOG_CONTROLLER,isRemember:IS_REMEMBER,autoLogin:AUTO_LOGIN}};
			this.listeners.submit != null && this.listeners.submit.call(this,event);
		}else{
			$("<p />").appendTo("#LOGIN_ERRORS").html(_that.errors.join("，"));
			
		}
		
}
/**
 * 获取模板路径
 */
Login.prototype.getViewPath = function(){
	return yijs.config.basePath+yijs.VIEWS+"/"+yijs.config.view+"/"+yijs.config.viewName.login;
}
/**
 * 点击登录后获取准备使用的微薄控制器
 */
Login.prototype.getController = function(controllerName){
	var Controller = null;
	switch (controllerName){
		case "Zuosa" : Controller = new Zuosa();break;
	}
	return Controller;
}
/**
 * 绑定事件
 * @param {string} type
 * @param {Object} fn
 */
Login.prototype.bind = function(type,fn){
	this.listeners[type] = fn;
},
/**
 * 移除事件
 * @param {string} type
 * @param {Object} fn
 */
Login.prototype.unbind = function(type,fn){
	this.listeners[type] = null;
}
